package Tools;

/**
 * 单位互转类
 * 
 * @author 紫夜
 * 
 */
public class Units {
	public static String byteCondense(long length) {
		int i = 0;
		while ((length / 1024) > 0) {
			length /= 1024;
			i++;
		}
		switch (i) {
		case 0:
			return length + "B";
		case 1:
			return length + "K";
		case 2:
			return length + "M";
		case 3:
			return length + "G";
		default:
			return length + "T";
		}
	}

	public static int byteCondensInt(long length) {
		int i = 0;
		while ((length / 1024) > 0) {
			length /= 1024;
			i++;
		}
		switch (i) {
		case 0:
			return (int) length;
		case 1:
			return (int) length;
		case 2:
			return (int) length;
		case 3:
			return (int) length;
		default:
			return (int) length;
		}
	}

	/**
	 * 延时
	 * 
	 * @param i
	 */
	public static void sleep(long time) {
		try {
			Thread.sleep(time);
		} catch (Exception e) {
		}

	}

	/**
	 * 将文本单位大小转换成long型
	 * 
	 * @param value
	 * @return
	 */
	public static long toByteLength(String value) {
		if (value == null || value.length() == 0) {
			return -1;
		}
		char u = value.toUpperCase().charAt(value.length() - 1);// 取单位

		long lon = 0;
		switch (u) {
		case 'B':
			lon = Long.parseLong(value.substring(0, value.length() - 1));
			break;
		case 'K':
			lon = Long.parseLong(value.substring(0, value.length() - 1)) * 1024;
			break;
		case 'M':
			lon = Long.parseLong(value.substring(0, value.length() - 1)) * 1024 * 1024;
			break;
		case 'G':
			lon = Long.parseLong(value.substring(0, value.length() - 1)) * 1024 * 1024 * 1024;
			break;
		case 'T':
			lon = Long.parseLong(value.substring(0, value.length() - 1)) * 1024 * 1024 * 1024 * 1024;
			break;
		default:
			if (u >= '0' && u <= '9')
				lon = Long.parseLong(value);
			else
				lon = -1;
		}
		return lon;
	}
}
